using UnityEngine;

/// <summary>
/// GameObject对象池工厂
/// </summary>
public class GameObjectPoolFactory : IPoolFactory<GameObject>
{
    private GameObject m_Prefab;
    private Transform m_Parent;
    
    public GameObjectPoolFactory(GameObject prefab, Transform parent = null)
    {
        m_Prefab = prefab;
        m_Parent = parent;
    }
    
    public GameObject Create()
    {
        var obj = Object.Instantiate(m_Prefab, m_Parent);
        obj.SetActive(false);
        return obj;
    }
    
    public void Destroy(GameObject obj)
    {
        if (obj != null)
        {
            Object.Destroy(obj);
        }
    }
    
    public void Reset(GameObject obj)
    {
        if (obj != null)
        {
            obj.SetActive(false);
            obj.transform.SetParent(m_Parent);
        }
    }
}

/// <summary>
/// MonoBehaviour对象池工厂
/// </summary>
/// <typeparam name="T">MonoBehaviour类型</typeparam>
public class MonoBehaviourPoolFactory<T> : IPoolFactory<T> where T : MonoBehaviour
{
    private T m_Prefab;
    private Transform m_Parent;
    
    public MonoBehaviourPoolFactory(T prefab, Transform parent = null)
    {
        m_Prefab = prefab;
        m_Parent = parent;
    }
    
    public T Create()
    {
        var obj = Object.Instantiate(m_Prefab, m_Parent);
        obj.gameObject.SetActive(false);
        return obj;
    }
    
    public void Destroy(T obj)
    {
        if (obj != null)
        {
            Object.Destroy(obj.gameObject);
        }
    }
    
    public void Reset(T obj)
    {
        if (obj != null)
        {
            obj.gameObject.SetActive(false);
            obj.transform.SetParent(m_Parent);
        }
    }
}

/// <summary>
/// 普通对象池工厂
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
public class ObjectPoolFactory<T> : IPoolFactory<T> where T : class, new()
{
    public T Create()
    {
        return new T();
    }
    
    public void Destroy(T obj)
    {
        // 普通对象不需要特殊销毁处理
    }
    
    public void Reset(T obj)
    {
        // 普通对象不需要特殊重置处理
    }
}
